#include "monitor.h"
#include "descriptor_tables.h"
#include "timer.h"
#include "paging.h"
#include "multiboot.h"
#include "fs.h"
#include "initrd.h"
#include "task.h"
#include "adapt.h"

extern u32int placement_address;
u32int initial_esp;



void task1(int i)
{
	printf(" Task1 say's: %d \n", i);
	for(;;){ }
}	

void task2(int i)
{
	printf(" Task2 say's: %d \n", i);
}

void xcore()
{
   Ping( strToIp("192.168.2.1"), 10, 1000 );
/*
	int a = 10;
	thread_create( (void*)task1, (void*)a);
	int b = 5;
	thread_create( (void*)task2, (void*)b);
	
	thread_create( (void*)thread_list, NULL);
*/	
}



void init_networking()
{

	ifconfig_t conf;
    	conf.ipaddr = inetAddress( 192, 168, 2, 6 );
    	conf.netmask = inetAddress( 255, 255, 255, 255 );
    	conf.gateway = inetAddress( 192, 168, 2, 1 );
    	conf.dns = inetAddress( 192, 168, 2, 1 );
    	conf.network = conf.ipaddr & conf.netmask;
    	conf.broadcast = conf.network | ( ~conf.netmask );
		

    	// tell and bind
    	printf( "Becoming %d.%d.%d.%d: ", IPAddr0( conf.ipaddr ), IPAddr1( conf.ipaddr ), IPAddr2( conf.ipaddr ), IPAddr3( conf.ipaddr ) );

		int status = netIfconfig( "0", &conf, 0 );
		
    	if( status != 0 )
    		printf( "FAIL\n" );
    	else
    		printf( "OK with status: %d\n", status );
			    asm volatile("sti");
   InitArpReplyWait();

  arpDisplayCache();

   

}


int main(struct multiboot *mboot_ptr, u32int initial_stack)
{
    initial_esp = initial_stack;
    // Initialise all the ISRs and segmentation
    monitor_clear();
	monitor_write("\n");
	printf("INIT IDT&GDT \n");
    init_descriptor_tables();
    // Initialise the screen (by clearing it)

	monitor_write_at(" xcore (Emiel van Goor, 2008)                                                   ",0,0, 7,8);

	
    // Initialise the PIT to 100Hz
	printf("INIT TIMER \n");
    init_timer(50);

    asm volatile("cli");

	
    // Find the location of our initial ramdisk.
    ASSERT(mboot_ptr->mods_count > 0);
    u32int initrd_location = *((u32int*)mboot_ptr->mods_addr);
    u32int initrd_end = *(u32int*)(mboot_ptr->mods_addr+4);
    // Don't trample our module with placement accesses, please!
    placement_address = initrd_end;



		printf("INIT PCI \n");
	InitPCI();
		printf("INIT NETWORK \n ");
    InitNe2k();	
	
	
    // Start paging.
		printf("INIT PAGING \n");
    initialise_paging();

    // Start multitasking.
		printf("INIT TASKING \n");
    initialise_tasking();

	
    // Initialise the initial ramdisk, and set it as the filesystem root.
//    fs_root = initialise_initrd(initrd_location);
    asm volatile("cli");
	
	thread_create(xcore, NULL);
	return 0;
}
